//
//  UserOrderDetailViewController.m
//  demoapp
//
//  Created by MingmingSun on 15/9/27.
//  Copyright (c) 2015年 pingplusplus. All rights reserved.
//

#import "UserOrderDetailViewController.h"
#import "KGModal.h"

@interface UserOrderDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,weak) IBOutlet UITableView *listView;
@property(nonatomic,strong) NSDictionary *chargeDic;

@property(nonatomic,strong) NSDictionary *orderDetailDic;
@property(nonatomic,strong) NSDictionary *couponDic;

@end

@implementation UserOrderDetailViewController

@synthesize listView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"订单详情";
        self.navigationItem.leftBarButtonItem =
        [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrow_white"]
                                         style:UIBarButtonItemStyleDone
                                        target:self
                                        action:@selector(backPressed:)];
    }
    return self;
}

-(void)backPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self regetOrderDetail];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if(!self.orderDetailDic)
        return 0;
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return 2;
        case 1:
            return 1;
        case 2:
            return 1;
        case 3:
            return 1;
        case 4:{
            NSInteger statusI = [[tooles replaceNullFromDic:self.orderDetailDic withKeyArray:@[@"status",@"code"]] integerValue];
            if(statusI == 500 || statusI == 600)
                return 4;
            return 1;
        }
        default:
            return 0;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10.0f;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger sectionI = indexPath.section;
    switch (sectionI) {
        case 0:
            switch (indexPath.row) {
                case 0:
                    return 100.0f;
                case 1:
                    return 40.0f;
                default:
                    return 0;
            }
        case 1:
            switch (indexPath.row) {
                case 0:
                    return 44.0f;
                default:
                    return 0;
            }
        case 2:
            switch (indexPath.row) {
                case 0:
                    return 50.0f;
                default:
                    return 0.0f;
            }
        case 3:
            return 50.0f;
        case 4:
            return 50.0f;
        default:
            return 0;
    }
    return 0;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    static NSString *inde;
    NSInteger sectionI = indexPath.section;
    switch (sectionI) {
        case 0:
            switch (indexPath.row) {
                case 0:{
                    inde = @"DDHeadID";
                    cell = [tableView dequeueReusableCellWithIdentifier:inde];
                    if (cell == nil) {
                        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DDHeadCell" owner:self options:nil];
                        cell = [nib objectAtIndex:0];
                    }
                    UILabel *lab1 = (UILabel*)[cell viewWithTag:1];
                    UILabel *lab2 = (UILabel*)[cell viewWithTag:2];
                    UIImageView *img99 = (UIImageView*)[cell viewWithTag:99];
                    lab1.text = [tooles replaceNullFromDic:self.orderDetailDic withKeyArray:@[@"barber"]];
                    lab2.text = [tooles replaceNullFromDic:self.orderDetailDic withKeyArray:@[@"store"]];
                }
                    break;
                case 1:{
                    inde = @"DDHeadDetailID";
                    cell = [tableView dequeueReusableCellWithIdentifier:inde];
                    if (cell == nil) {
                        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DDHeadDetail" owner:self options:nil];
                        cell = [nib objectAtIndex:0];
                    }
                    UILabel *lab1 = (UILabel*)[cell viewWithTag:1];
                    UILabel *lab2 = (UILabel*)[cell viewWithTag:2];
                    lab1.text = [NSString stringWithFormat:@"预约时间\n%@",[[self.orderDetailDic valueForKey:@"scheduleDate"] substringToIndex:10]];
                    lab2.text = [NSString stringWithFormat:@"%@-%@",[tooles replaceNullFromDic:self.orderDetailDic withKeyArray:@[@"gender"]],[tooles replaceNullFromDic:self.orderDetailDic withKeyArray:@[@"hairType"]]];
                }
                    break;
                default:
                    break;
            }
            break;
        case 1:{
            inde = @"DDTypeID";
            cell = [tableView dequeueReusableCellWithIdentifier:inde];
            if (cell == nil) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DDType" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            UILabel *lab1 = (UILabel*)[cell viewWithTag:1];
            UILabel *lab2 = (UILabel*)[cell viewWithTag:2];
            lab1.text = @"服务类型";
            lab2.text = [tooles replaceNullFromDic:self.orderDic withKeyArray:@[@"services"]];
        }
            break;
        case 2:{
            inde = @"DDPriceTitleID";
            cell = [tableView dequeueReusableCellWithIdentifier:inde];
            if (cell == nil) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DDPriceTItle" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            UILabel *lab2 = (UILabel*)[cell viewWithTag:2];
            lab2.text = [NSString stringWithFormat:@"￥%.2f",[[self.orderDic valueForKey:@"dealPrice"] floatValue]];
        }
            break;
        case 3:{
            inde = @"DDPriceTitleID";
            cell = [tableView dequeueReusableCellWithIdentifier:inde];
            if (cell == nil) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DDPriceTItle" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            UILabel *lab1 = (UILabel*)[cell viewWithTag:1];
            UILabel *lab2 = (UILabel*)[cell viewWithTag:2];
            lab1.text = @"优惠券";
            if(self.couponDic){
                lab2.text = [NSString stringWithFormat:@"-￥%.2f",[[self.couponDic valueForKey:@"price"] floatValue]];
            }else{
                lab2.text = @"未选择优惠券";
            }
        }
            break;
        case 4:{
            inde = @"DDTypeID";
            cell = [tableView dequeueReusableCellWithIdentifier:inde];
            if (cell == nil) {
                NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DDType" owner:self options:nil];
                cell = [nib objectAtIndex:0];
            }
            UILabel *lab1 = (UILabel*)[cell viewWithTag:1];
            UILabel *lab2 = (UILabel*)[cell viewWithTag:2];
            switch (indexPath.row) {
                case 0:
                    lab1.text = @"订单状态";
                    lab2.text = [tooles replaceNullFromDic:self.orderDetailDic withKeyArray:@[@"status",@"name"]];
                    break;
                case 1:{
                    lab1.text = @"专业";
                    lab2.text = [NSString stringWithFormat:@"%i",[[self.orderDetailDic valueForKey:@"barberProfession"] integerValue]];
                }
                    break;
                case 2:{
                    lab1.text = @"沟通";
                    lab2.text = [NSString stringWithFormat:@"%i",[[self.orderDetailDic valueForKey:@"barberCommunicate"] integerValue]];
                }
                    break;
                case 3:{
                    lab1.text = @"守时";
                    lab2.text = [NSString stringWithFormat:@"%i",[[self.orderDetailDic valueForKey:@"barberPunctual"] integerValue]];
                }
                    break;
                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    return;
}

-(void)regetOrderDetail{
    NSDictionary *para =
    @{@"id":[NSString stringWithFormat:@"%@",[self.orderDic valueForKey:@"id"]],
      @"token":APPALL.myUserItem.token};
    [SVProgressHUD showWithStatus:@"loading..."];
    [AVCloud callFunctionInBackground:@"order_index" withParameters:para block:^(id object, NSError *error) {
        // 执行结果
        if (!error && object) {
            NSLog(@"avget:%@",object);
            NSDictionary *aSucData = [NSJSONSerialization JSONObjectWithData: [object dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: &error];
            switch ([[aSucData valueForKey:@"success"] intValue]) {
                case 0:
                {
                    [tooles showGlobalAlertWithTitle:@"" andMsg:[aSucData valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [self.navigationController popViewControllerAnimated:YES];
                }
                    break;
                case 1:
                {
                    self.orderDetailDic = [aSucData objectForKey:@"order"];
                    [self getOrderCoupon];
                }
                    break;
                default:
                    break;
            }
            [SVProgressHUD dismiss];
        } else {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
        }
    }];
}

-(void)getOrderCoupon{
    NSString *couponID = [self.orderDetailDic valueForKey:@"coupon"];
    if([couponID isEqual:[NSNull null]] ||
       !couponID){
        [self.listView reloadData];
        return;
    }
    NSDictionary *para =
    @{@"id":[NSString stringWithFormat:@"%@",couponID],
      @"token":APPALL.myUserItem.token};
    [SVProgressHUD showWithStatus:@"loading..."];
    [AVCloud callFunctionInBackground:@"coupon_index" withParameters:para block:^(id object, NSError *error) {
        // 执行结果
        if (!error && object) {
            NSLog(@"avget:%@",object);
            NSDictionary *aSucData = [NSJSONSerialization JSONObjectWithData: [object dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: &error];
            switch ([[aSucData valueForKey:@"success"] intValue]) {
                case 0:
                {
                    [tooles showGlobalAlertWithTitle:@"" andMsg:[aSucData valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [self.navigationController popViewControllerAnimated:YES];
                }
                    break;
                case 1:
                {
                    self.couponDic = [aSucData objectForKey:@"coupon"];
                    [self.listView reloadData];
                }
                    break;
                default:
                    break;
            }
            [SVProgressHUD dismiss];
        } else {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
        }
    }];
    
}

@end
